package com.xilihui.xlh.business.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.AreaEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 2017/4/11.
 */

public class AddressPickerDialog implements View.OnClickListener {

    private RxAppCompatActivity context;
    private Dialog dialog;
    private AddressPickerDialog.Callback callback;

    private List<AreaEntity.DataBean> provinces = new ArrayList<>();
    private List<AreaEntity.DataBean.SubBeanX> cities = new ArrayList<>();

    private LoopView picker_province;
    private LoopView picker_city;
    private LoopView picker_county;

    private String province;
    private String provinceId;
    private String city;
    private String cityId;
    private String county;
    private String countyId;

    private Layer layer = Layer.THREE;

    public enum Layer {
        TWO, THREE
    }

    public AddressPickerDialog(RxAppCompatActivity context) {

        this.context = context;
    }

    public AddressPickerDialog setCallback(AddressPickerDialog.Callback callback) {

        this.callback = callback;
        return this;
    }

    public AddressPickerDialog setData(AreaEntity entity) {

        if (entity != null) {
            provinces.clear();
            provinces.addAll(entity.getData());
            province = provinces.get(0).getName();
            provinceId = provinces.get(0).getId();
            cities.clear();
            cities.addAll(entity.getData().get(0).getSub());
            city = cities.get(0).getName();
            cityId = cities.get(0).getId();

            if (layer == Layer.THREE) {
                List<AreaEntity.DataBean.SubBeanX.SubBean> counties = cities.get(0).getSub();
                if (counties.size() > 0) {
                    county = counties.get(0).getName();
                    countyId = counties.get(0).getId();
                }
            }
        }
        return this;
    }

    public AddressPickerDialog setLayer(Layer layer) {
        this.layer = layer;
        return this;
    }

    public void show() {

        View view = LayoutInflater.from(context).inflate(R.layout.address_picker, null);

        view.findViewById(R.id.tvConfirm).setOnClickListener(this);
        view.findViewById(R.id.tvCancel).setOnClickListener(this);

        picker_province = (LoopView) view.findViewById(R.id.picker_province);
        picker_city = (LoopView) view.findViewById(R.id.picker_city);
        picker_county = (LoopView) view.findViewById(R.id.picker_county);
        if (layer == Layer.THREE) {
            picker_county.setVisibility(View.VISIBLE);
        } else {
            picker_county.setVisibility(View.GONE);
        }

        initProvince();
        initCity();
        if (layer == Layer.THREE) {
            initCounty(0);
        }

        dialog = new Dialog(context, R.style.Dialog);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        Window window = dialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = getScreenWidth(context);
            lp.height = (int) (getScreenHeight(context) * 0.4);
            window.setAttributes(lp);
        }
        dialog.show();
    }

    private void initCounty(final int index) {
        ArrayList<String> countyStrs = new ArrayList<>();
        if (!cities.isEmpty()) {
            final AreaEntity.DataBean.SubBeanX city = cities.get(index);

            if (city.getSub() == null || city.getSub().isEmpty()) {
                county = "";
                countyId = null;
                picker_county.setItems(null);
            } else {
                for (AreaEntity.DataBean.SubBeanX.SubBean county : city.getSub()) {
                    countyStrs.add(county.getName());
                }
                picker_county.setItems(countyStrs);
                picker_county.setCurrentPosition(0);
                picker_county.setListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(int index1) {
                        county = city.getSub().get(index1).getName();
                        countyId = city.getSub().get(index1).getId();
                    }
                });
            }
        }
    }

    private void initCity() {
        ArrayList<String> cityStrs = new ArrayList<>();
        for (AreaEntity.DataBean.SubBeanX city : cities) {
            cityStrs.add(city.getName());
        }
//        if (cityStrs.size() <= 1) {
//            picker_city.setCanLoop(false);
//        } else {
//            picker_city.setCanLoop(true);
//        }

        picker_city.setItems(cityStrs);
        picker_city.setCurrentPosition(0);
        picker_city.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {


                AreaEntity.DataBean.SubBeanX city = cities.get(index);
                AddressPickerDialog.this.city = city.getName();
                AddressPickerDialog.this.cityId = city.getId();

                if (layer == Layer.THREE) {

                    if (city.getSub() != null && !city.getSub().isEmpty()) {
                        county = city.getSub().get(0).getName();
                        countyId = city.getSub().get(0).getId();
                    }
                    initCounty(index);
                }
            }
        });
    }

    private void initProvince() {

        ArrayList<String> provinceStrs = new ArrayList<>();
        for (AreaEntity.DataBean province : provinces) {
            provinceStrs.add(province.getName());
        }
//        if (provinceStrs.size() <= 1) {
//            picker_province.setCanLoop(false);
//        } else {
//            picker_province.setCanLoop(true);
//        }
        picker_province.setItems(provinceStrs);
        picker_province.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

                province = provinces.get(index).getName();
                provinceId = provinces.get(index).getId();
                cities.clear();
                cities.addAll(provinces.get(index).getSub());

                AreaEntity.DataBean.SubBeanX city = cities.get(0);
                AddressPickerDialog.this.city = city.getName();
                AddressPickerDialog.this.cityId = city.getId();

                initCity();

                if (layer == Layer.THREE) {

                    picker_county.setCurrentPosition(0);

                    county = city.getSub().get(0).getName();
                    countyId = city.getSub().get(0).getId();
                    initCounty(0);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvConfirm:
                if (callback != null) {
                    callback.onSelected(province, city, county, provinceId, cityId, countyId);
                }
                dialog.dismiss();
                break;
            case R.id.tvCancel:
                if (dialog != null) {
                    dialog.dismiss();
                }
                break;
        }
    }

    public interface Callback {
        void onSelected(String province, String city, String county, String provinceId, String cityId, String countyId);
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕高度
     *
     * @param context
     * @return
     */
    private int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }
}
